package com.softtown.modules.dapai.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.common.utils.TreeUtils;
import com.softtown.modules.dapai.dao.CompCityDao;
import com.softtown.modules.dapai.dto.CompCityDTO;
import com.softtown.modules.dapai.entity.CompCityEntity;
import com.softtown.modules.dapai.service.CompCityService;
import com.softtown.modules.dapai.vo.CompCityVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class CompCityServiceImpl extends
        BaseServiceImpl<CompCityDao, CompCityEntity> implements CompCityService {

    @Override
    public List<CompCityVO> getTreeCity(Long companyId) {

        List<CompCityEntity> cityEntities = baseDao.getCityTree(companyId);
        return TreeUtils.build(ConvertUtils.sourceToTarget(cityEntities, CompCityVO.class));
    }

    @Override
    public List<CompCityVO> getProvinceCities(Long compId) {
        return ConvertUtils.sourceToTarget( baseDao.getProvinceCities(compId), CompCityVO.class);
    }

    @Override
    public List<CompCityVO> getCityListById(Long compId, Long id) {
        return ConvertUtils.sourceToTarget( baseDao.getCityListById(compId,id), CompCityVO.class);
    }

    @Override
    public List<CompCityVO> getCityList(Long companyId) {
        List<CompCityEntity> orgListByCompId = baseDao.getCityList(companyId);
        return ConvertUtils.sourceToTarget(orgListByCompId, CompCityVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCity(CompCityDTO compCityDTO) {
        CompCityEntity compCityEntity = ConvertUtils
                .sourceToTarget(compCityDTO, CompCityEntity.class);
        insert(compCityEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCity(CompCityDTO compCityDTO) {
        CompCityEntity compCityEntity = ConvertUtils
                .sourceToTarget(compCityDTO, CompCityEntity.class);
        compCityEntity.setIsDeleted(IsDeleteEnum.NORMAL.value());
        baseDao.updateCity(compCityEntity);
    }

    @Override
    public void deleteCity(Long compId, Long id) {
        CompCityEntity compCityEntity = baseDao.selectOne(new QueryWrapper<CompCityEntity>().lambda().eq(CompCityEntity::getCompId,compId)
        .eq(CompCityEntity::getId,id));
        compCityEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        baseDao.updateCity(compCityEntity);
    }
}
